<template>
  <div class="user-statistics-container">
    <h2>用户统计</h2>
    
    <!-- 日期选择器 -->
    <div class="date-filter">
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="fetchData"
      ></el-date-picker>
    </div>
    
    <!-- 数据概览卡片 -->
    <el-row :gutter="20" class="overview-cards">
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="card-content">
            <div class="card-title">总用户数</div>
            <div class="card-value">{{ overviewData.totalUsers }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="card-content">
            <div class="card-title">新增用户</div>
            <div class="card-value">{{ overviewData.newUsers }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="card-content">
            <div class="card-title">活跃用户</div>
            <div class="card-value">{{ overviewData.activeUsers }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="card-content">
            <div class="card-title">平均年龄</div>
            <div class="card-value">{{ overviewData.avgAge }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-area">
      <!-- 新增用户趋势图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div class="chart-title">新增用户趋势</div>
          <div class="chart-container">
            <line-chart :chart-data="newUserTrendData" />
          </div>
        </el-card>
      </el-col>
      
      <!-- 活跃用户趋势图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div class="chart-title">活跃用户趋势</div>
          <div class="chart-container">
            <line-chart :chart-data="activeUserTrendData" />
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="chart-area">
      <!-- 性别分布图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div class="chart-title">性别分布</div>
          <div class="chart-container">
            <pie-chart :chart-data="genderDistributionData" />
          </div>
        </el-card>
      </el-col>
      
      <!-- 年龄分布图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div class="chart-title">年龄分布</div>
          <div class="chart-container">
            <el-row>
              <el-col>
                <el-card>
                  <!-- 传递模拟的年龄分布数据给 BarChart 组件 -->
                  <BarChart :chartData="ageDistributionData" />
                </el-card>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import LineChart from './charts/LineChart.vue'
import PieChart from './charts/PieChart.vue'
import BarChart from './charts/BarChart.vue'

export default {
  name: 'UserStatistics',
  components: {
    LineChart,
    PieChart,
    BarChart
  },
  data() {
    return {
      dateRange: [new Date(new Date().setDate(new Date().getDate() - 30)), new Date()],
      overviewData: {
        totalUsers: 0,
        newUsers: 0,
        activeUsers: 0,
        avgAge: 0
      },
      newUserTrendData: {
        dates: [],
        counts: []
      },
      activeUserTrendData: {
        dates: [],
        counts: []
      },
      genderDistributionData: {
        labels: ['男', '女'],  // 移除了"其他"选项
        data: []
      },
      ageDistributionData: {
        labels: ['<18', '18-25', '26-35', '36-45', '46-55', '>55'],
        data: []
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // 模拟数据获取
      this.getOverviewData()
      this.getTrendData()
      this.getDistributionData()
    },
    getOverviewData() {
      // 模拟API调用
      setTimeout(() => {
        this.overviewData = {
          totalUsers: 1254,
          newUsers: 42,
          activeUsers: 356,
          avgAge: 32.5
        }
      }, 500)
    },
    getTrendData() {
      // 模拟API调用
      setTimeout(() => {
        const dates = []
        const newUserCounts = []
        const activeUserCounts = []
        
        for (let i = 0; i < 30; i++) {
          const date = new Date(new Date().setDate(new Date().getDate() - 30 + i))
          dates.push(`${date.getMonth() + 1}/${date.getDate()}`)
          newUserCounts.push(Math.floor(Math.random() * 20) + 10)
          activeUserCounts.push(Math.floor(Math.random() * 100) + 50)
        }
        
        this.newUserTrendData = {
          dates,
          counts: newUserCounts
        }
        
        this.activeUserTrendData = {
          dates,
          counts: activeUserCounts
        }
      }, 500)
    },
    getDistributionData() {
      // 模拟API调用
      setTimeout(() => {
        this.genderDistributionData = {
          labels: ['男', '女'],  // 移除了"其他"选项
          data: [48, 52]  // 调整了数据比例，总和保持100
        }
        
        this.ageDistributionData = {
          labels: ['<18', '18-25', '26-35', '36-45', '46-55', '>55'],
          data: [8, 32, 28, 18, 10, 4]
        }
      }, 500)
    }
  }
}
</script>

<style scoped>
.user-statistics-container {
  padding: 20px;
}

h2 {
  margin-bottom: 20px;
  color: #333;
}

.date-filter {
  margin-bottom: 20px;
}

.overview-cards {
  margin-bottom: 20px;
}

.card-content {
  text-align: center;
}

.card-title {
  font-size: 14px;
  color: #999;
  margin-bottom: 10px;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.chart-area {
  margin-bottom: 20px;
}

.chart-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
  text-align: center;
}

.chart-container {
  height: 300px;
}
</style>